//
//  HNMyViewController.m
//  JHFBaseProject
//
//  Created by 朱传正 on 2016/12/13.
//  Copyright © 2016年 好牛. All rights reserved.
//

#import "HNMyViewController.h"
#import "HNSettingViewController.h"
#import "HNMyOrderViewController.h"
#import "HNConcernViewController.h"
#import "HNConsultController.h"
#import "HNAboutViewController.h"

@interface HNMyViewController ()


@property (nonatomic,assign)CGFloat contentHeight;

@property (nonatomic,strong)NSDictionary *userInfo;

@end

@implementation HNMyViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
      [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self updateUserInfo];
}


-(void)updateUserInfo{
    NSDictionary *userInfo = [AppConfig getUserInfo];
    NSLog(@"%@",userInfo);
    
    NSString *imageStr = [NSString stringWithFormat:@"%@%@",url_Base,[userInfo objectForKey:@"photo"]];
    [_headHeadImage sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@"D99GN4P63Q4F.png"]];
    
    _headTel.text = [NSString stringWithFormat:@"%@",[userInfo objectForKey:@"hnLoginName"]];
    
    _headName.text = [NSString stringWithFormat:@"%@",[userInfo objectForKey:@"nickname"]];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}



- (IBAction)settingAction:(UIButton *)sender {
    HNSettingViewController *settingVC = [[ HNSettingViewController alloc]init];
    [self.navigationController pushViewController:settingVC animated:YES];
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}


-(void)drawView{
    
    _headHeadImage.layer.masksToBounds = YES;
    _headHeadImage.layer.cornerRadius = _headHeadImage.frame.size.height/2;
    
    _contentHeight = 180;
    
    _cellVlew.backgroundColor = RGB(236, 240, 241);
    
    //头部行
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, _contentHeight, kScreen_Width, 50)];
    headView.backgroundColor = [UIColor whiteColor];
    [_cellVlew addSubview:headView];
    
    _contentHeight = _contentHeight + headView.frame.size.height;
    
    UIImageView *signImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];
    signImageView.image = [UIImage imageNamed:@"(编组).png"];
    [headView addSubview:signImageView];
    
    //标题
    UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(60,15,100,20)];
    nameLabel.numberOfLines = 1;
    nameLabel.clipsToBounds = YES;
    nameLabel.textAlignment = NSTextAlignmentLeft;
    nameLabel.font = [UIFont systemFontOfSize:15];
    
    nameLabel.textColor = RGBJ(10);
    nameLabel.text = @"我的订单";
    [headView addSubview:nameLabel];
    
    //标题
    UILabel *rightLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreen_Width-100 ,10,90,30)];
    rightLabel.numberOfLines = 1;
    rightLabel.clipsToBounds = YES;
    rightLabel.layer.cornerRadius = 15;
    rightLabel.textAlignment = NSTextAlignmentCenter;
    rightLabel.font = [UIFont systemFontOfSize:15];
    rightLabel.backgroundColor = RGB(84, 204, 83);
    rightLabel.textColor = [UIColor whiteColor];
    rightLabel.text = @"咨询订单";
    [headView addSubview:rightLabel];
    
    //添加点击按钮——覆盖在原来的View上
    UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [clickBtn setTitle:@"" forState:UIControlStateNormal];
    [clickBtn setBackgroundColor:RGBA(255, 255, 255, 0)];
    [clickBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    clickBtn.tag = 1000 + 10;
    clickBtn.frame = nameLabel.frame;//大小和位置
    [clickBtn addTarget:self action:@selector(enterMyOrder:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:clickBtn];
    //添加点击按钮——覆盖在原来的View上
    UIButton *clickBtn2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [clickBtn2 setTitle:@"" forState:UIControlStateNormal];
    [clickBtn2 setBackgroundColor:RGBA(255, 255, 255, 0)];
    [clickBtn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    clickBtn2.frame = rightLabel.frame;//大小和位置
    [clickBtn2 addTarget:self action:@selector(enterConsultOrder) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:clickBtn2];
    
    UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, _contentHeight - 0.5, kScreen_Width, 0.5)];
    lineLabel.backgroundColor = [UIColor colorWithHexString:@"cecece"];
    [_cellVlew addSubview:lineLabel];
    
    NSArray *imageArray = [NSArray arrayWithObjects:@"Wallet.png",@"552cc4649bf52_128.png",@"552cc3f38c1b4_128.png",@"552cc582248d4_128.png", nil];
    NSArray *nameArray = [NSArray arrayWithObjects:@"待付款",@"待审核",@"已发货",@"已收货", nil];
    UIView *categoryView = [[UIView alloc]initWithFrame:CGRectMake(0, _contentHeight, kScreen_Width, 80)];
    [_cellVlew addSubview:categoryView];
    
    _contentHeight = _contentHeight + categoryView.frame.size.height;
    for (NSInteger i = 0; i<imageArray.count; i++) {
        
        UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake(kScreen_Width*i/4,0, kScreen_Width/4, categoryView.frame.size.height)];
        itemView.backgroundColor = [UIColor whiteColor];
        [categoryView addSubview:itemView];
        
        //图像
        UIImageView *signImageView = [[UIImageView alloc]initWithFrame:CGRectMake(itemView.frame.size.width/2-11,16,22,22)];
        signImageView.image = [UIImage imageNamed:imageArray[i]];
        [itemView addSubview:signImageView];
        
        //标题
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(0,signImageView.frame.origin.y + signImageView.frame.size.height,itemView.frame.size.width,20)];
        nameLabel.numberOfLines = 1;
        nameLabel.clipsToBounds = YES;
        nameLabel.textColor = RGBJ(160);
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.font = [UIFont systemFontOfSize:13];
    
        nameLabel.text = nameArray[i];
        [itemView addSubview:nameLabel];
        
   

    
       
        //添加点击按钮——覆盖在原来的View上
        UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [clickBtn setTitle:@"" forState:UIControlStateNormal];
        [clickBtn setBackgroundColor:RGBA(255, 255, 255, 0)];
        clickBtn.tag = 1000 + i;
        [clickBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        clickBtn.frame = itemView.frame;//大小和位置
        [clickBtn addTarget:self action:@selector(enterMyOrder:) forControlEvents:UIControlEventTouchUpInside];
        [categoryView addSubview:clickBtn];
      
    }
      _contentHeight = _contentHeight + 5;
    
    NSArray *imageArr = [NSArray arrayWithObjects:@"2.png",@"3.png",@"白.png",@"图层-39.png", nil];
     NSArray *nameArr = [NSArray arrayWithObjects:@"我的关注",@"咨询记录",@"关于我们",@"全国服务热线：888-88888888", nil];
    for (int i = 0; i<imageArr.count; i++) {
        UIView *itemView = [[UIView alloc]initWithFrame:CGRectMake(0, _contentHeight, kScreen_Width, 50)];
        itemView.backgroundColor = [UIColor whiteColor];
        [_cellVlew addSubview:itemView];
        
        _contentHeight = _contentHeight + itemView.frame.size.height;
        
        UIImageView *signImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 30, 30)];
        signImageView.image = [UIImage imageNamed:imageArr[i]];
        [itemView addSubview:signImageView];
        
        //标题
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(60,15,kScreen_Width- 100,20)];
        nameLabel.numberOfLines = 1;
        nameLabel.clipsToBounds = YES;
        nameLabel.textAlignment = NSTextAlignmentLeft;
        nameLabel.font = [UIFont systemFontOfSize:15];
        
       nameLabel.textColor = RGBJ(10);
        nameLabel.text = nameArr[i];
        [itemView addSubview:nameLabel];
        
        if (i != nameArr.count - 1) {
            UIImageView *rightImageView = [[UIImageView alloc]initWithFrame:CGRectMake(kScreen_Width - 35, 15, 20, 20)];
            rightImageView.image = [UIImage imageNamed:@"rightBtn.png"];
            [itemView addSubview:rightImageView];
            UILabel *lineLabel2 = [[UILabel alloc]initWithFrame:CGRectMake(0,itemView.frame.size.height-1, kScreen_Width, 0.5)];
            lineLabel2.backgroundColor = [UIColor colorWithHexString:@"cecece"];
            [itemView addSubview:lineLabel2];
            [itemView bringSubviewToFront:lineLabel2];
          
        }
        //添加点击按钮——覆盖在原来的View上
        UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [clickBtn setTitle:@"" forState:UIControlStateNormal];
        [clickBtn setBackgroundColor:RGBA(255, 255, 255, 0)];
        clickBtn.tag = 100 + i;
        [clickBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        clickBtn.frame = itemView.frame;//大小和位置
        [clickBtn addTarget:self action:@selector(enterOtherVC:) forControlEvents:UIControlEventTouchUpInside];
        [_cellVlew addSubview:clickBtn];    }
}

-(void)initData{
    [self getUserInfo];
}

#pragma mark - 进入我的订单
-(void)enterMyOrder:(UIButton *)btn{
    
    NSInteger tag = btn.tag - 1000;
    HNMyOrderViewController *myOrderVC = [[HNMyOrderViewController alloc]init];
    myOrderVC.title = @"我的订单";
    myOrderVC.orderType = @"my";
    if (tag == 10) {
        myOrderVC.choiceState = 0;
        [self.navigationController pushViewController:myOrderVC animated:YES];
    }else{
        myOrderVC.choiceState = tag + 1;
        switch (tag) {
            case 0:
                [self.navigationController pushViewController:myOrderVC animated:YES];
                break;
            case 1:
                 [self.navigationController pushViewController:myOrderVC animated:YES];
                break;
            case 2:
                 [self.navigationController pushViewController:myOrderVC animated:YES];
                break;
            case 3:
                 [self.navigationController pushViewController:myOrderVC animated:YES];
                break;
            default:
                break;
        }
    }
    
}


#pragma mark - 进入咨询订单
-(void)enterConsultOrder{
    HNMyOrderViewController *myOrderVC = [[HNMyOrderViewController alloc]init];
     myOrderVC.title = @"咨询订单";
    [self.navigationController pushViewController:myOrderVC animated:YES];
}


#pragma mark - 进入页面

-(void)enterOtherVC:(UIButton *)btn{
    NSInteger tag = btn.tag - 100;
       HNConcernViewController *concernVC = [[HNConcernViewController alloc]init];
     HNConsultController *consultVC = [[HNConsultController alloc]init];
     HNAboutViewController *aboutVC = [[HNAboutViewController alloc]init];
     NSString *phoneNumberURLString = [@"tel://" stringByAppendingString:@"888-88888888"];
    switch (tag) {
        case 0:
             concernVC.title = @"我的关注";
             [self.navigationController pushViewController:concernVC animated:YES];
            break;
        case 1:
             consultVC.title = @"咨询服务";
             [self.navigationController pushViewController:consultVC animated:YES];
            break;
            
        case 2:
             aboutVC.title = @"关于我们";
             [self.navigationController pushViewController:aboutVC animated:YES];
            break;
        case 3:
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumberURLString]];
            break;
        default:
            break;
    }
}


-(void)getUserInfo{
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    
    NSLog(@"param = %@",param);
    
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_userInfo_detail parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSLog(@"responseObject = %@",responseObject);
        
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"获取用户信息失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
            _userInfo = [NSDictionary dictionaryWithDictionary:[data objectForKey:@"obj"]];
            NSLog(@"------obj = %@",_userInfo);
            
            NSString *userImageStr = [NSString stringWithFormat:@"%@",[_userInfo objectForKey:@"photo"]];
            if ([AppConfig isNULL:userImageStr]) {
                userImageStr = @"null";
            }
            NSString *userNickname = [NSString stringWithFormat:@"%@",[_userInfo objectForKey:@"nickname"]];
            NSLog(@"%@",userNickname);
            
            if ([AppConfig isNULL:userNickname]) {
                
                userNickname = @"未设置";
            }

            
            NSString *userUserName = [NSString stringWithFormat:@"%@",[_userInfo objectForKey:@"username"]];
            if ([AppConfig isNULL:userUserName]) {
                userImageStr = @"未设置";
            }
            NSDictionary *saveDic = [NSDictionary dictionaryWithObjectsAndKeys:userImageStr,@"photo",userUserName,@"hnLoginName",userNickname,@"nickname", nil];
            
            [AppConfig saveUserInfo:saveDic];
        
            NSString *imageStr = [NSString stringWithFormat:@"%@%@",url_Base,userImageStr];
            [_headHeadImage sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@"D99GN4P63Q4F.png"]];
            
            _headTel.text = userUserName;
            
            _headName.text = userNickname;
            
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"msg"]];
            NSLog(@"%@",info);
            
            if ([info isEqualToString:msg_InvalidLoginInformation]) {
                
                [AppConfig removeUserInfo];
                
                [[EMClient sharedClient] logout:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@NO];
                
                LoginViewController *loginVC = [[LoginViewController alloc]init];
                
                [self.navigationController pushViewController:loginVC animated:YES];
            }
            NSLog(@"%@",info);
            
            
            [self showHint:info];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull  error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"获取用户信息失败"];
    }];
    
    
}



@end
